package 第二十章项目.clothesSystem.utils;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.Xpp3Driver;
import org.junit.Test;
import 第二十章项目.clothesSystem.bean.Clothes;
import 第二十章项目.clothesSystem.bean.Order;
import 第二十章项目.clothesSystem.bean.OrderItem;
import 第二十章项目.clothesSystem.bean.User;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class XMLPaser_new {
    @Test
    public void xpp(){
        List<Clothes> postlist = getpostlist();
        XStream xstream=new XStream(new Xpp3Driver());

        xstream.allowTypes(new Class[] {Clothes.class, Order.class, OrderItem.class, User.class });
        xstream.alias("服装类", Clothes.class);
        xstream.useAttributeFor(Clothes.class,"id");
        //xstream.addImplicitCollection(ShellofClothes.class,"clotheslist");
        //xstream.aliasField("Product",Clothes.class, "list");
        try {
            BufferedOutputStream outputStream = null;
            BufferedInputStream inputStream=null;
            File file=new File("."+"\\"+"products.xml");
            if(!file.exists()) {
                outputStream=new BufferedOutputStream(
                        new FileOutputStream(file,true));
                outputStream.write("<?xml version=\"1.0\" encoding=\"utf-8\" ?>".getBytes());
                xstream.toXML(postlist, outputStream);
                outputStream.close();
                System.out.println("写入完毕!");
            }else{
                System.out.println("文件已经存在,路径是: "+file.getAbsolutePath());
            }
            if(file.exists()) {
                inputStream = new BufferedInputStream(new FileInputStream(file));
                final Object xml_str = xstream.fromXML(inputStream);
                List<Clothes>product=(List<Clothes>)xml_str;
                System.out.println("文件读取开始: ");
                product.stream().forEach(System.out::println);
                System.out.println("文件读取结束! ");
            }else{
                System.out.println("文件不存在.");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static List<Clothes> xml_to_list() {
        XStream xstream = new XStream(new Xpp3Driver());
        xstream.allowTypes(new Class[]{Clothes.class, Order.class, OrderItem.class, User.class});
        xstream.alias("服装类", Clothes.class);
        xstream.useAttributeFor(Clothes.class,"id");
        File file = new File("." + "\\" + "products.xml");
        if (file.exists()) {
            BufferedInputStream inputStream = null;
            try
                {
                    inputStream = new BufferedInputStream(new FileInputStream(file));
                    final Object xml_str = xstream.fromXML(inputStream);
                    List<Clothes> product = (List<Clothes>) xml_str;
                    return product;
                } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            }
        }else{
            System.out.println("can't find file!");
        }
        return null;
    }

    public static List<Clothes> getpostlist(){
        List<Clothes > paser_list=new ArrayList<>();
        paser_list.add(new Clothes("A001","Hermes","Dresses","brown","L",42,2274.12,"2023 summer release"));
        paser_list.add(new Clothes("A002","Louis Vuitton","Bag","Light green","S",4,1274.99,"2023 Fall drainage"));
        paser_list.add(new Clothes("A003","Prada","Trousers","Light white","S",45,1999.99,"2023 summer sales"));
        paser_list.add(new Clothes("A004","Chanel","Perfume","Pale yellow","M",74,3274.02,"2023 summer style"));
        paser_list.add(new Clothes("A005","Dior","Mascara brush","Light red","S",13,6274.01,"2023 Winter drainage"));
        paser_list.add(new Clothes("A006","Gucci","bracelet","silver","S",2,8276.06,"2024 summer earnings"));
        paser_list.add(new Clothes("A007","Armani","Dresses","lacery","M",9,2374.04,"2024 Summer release"));
        paser_list.add(new Clothes("A008","Versace","T-shirt","Fancy yellow","S",67,3212.44,"Summer 2024 fashion"));
        paser_list.add(new Clothes("A009","Burberry","Women's pants","Navy blue","L",12,7974.67,"2024 summer earnings"));
        paser_list.add(new Clothes("A010","Givenchy","Men's trench coat","brown","S",31,2334.23,"Spring 2024 fashion"));
        //paser_list.stream().forEach(System.out::println);
        return paser_list;
    }
}
